package org.yylnb.openplatform.webmaster.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yylnb.openplatform.commons.bean.FunctionType;
import org.yylnb.openplatform.commons.bean.NotifyMQBean;
import org.yylnb.openplatform.commons.bean.OperatorType;
import org.yylnb.openplatform.commons.constans.ApiTagInRedis;
import org.yylnb.openplatform.webmaster.entity.ApiMapping;
import org.yylnb.openplatform.webmaster.mapper.ApiMappingMapper;
import org.yylnb.openplatform.webmaster.mq.NotifyGateWayMQBean;
import org.yylnb.openplatform.webmaster.service.ApiMappingService;
import org.yylnb.openplatform.webmaster.service.api.CacheService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Service
@Transactional
public class ApiMappingServiceImpl implements ApiMappingService {

    @Autowired
    private  ApiMappingMapper apiMappingMapper;


    @Autowired
    private CacheService cacheService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private NotifyGateWayMQBean notifyGateWayMQBean;




    @Override
    public void addApiMapping(ApiMapping mapping) {
        apiMappingMapper.addApiMapping(mapping);
        //添加到缓存
        if (mapping.getState() == 1) {
            try {
                cacheService.hSets(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName(), objectMapper.readValue(objectMapper.writeValueAsString(mapping), Map.class));
                //通知网关发生了变化.添加了一个路由信息

                MessageChannel channel = notifyGateWayMQBean.message_channel();
                NotifyMQBean notifyMQBean = new NotifyMQBean();
                notifyMQBean.setKey(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName());
                notifyMQBean.setFunctionType(FunctionType.API_MAPPING);
                notifyMQBean.setOperatorType(OperatorType.ADD);
                channel.send(new GenericMessage<NotifyMQBean>(notifyMQBean));


            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void updateApiMapping(ApiMapping mapping) {
        apiMappingMapper.updateApiMapping(mapping);
        //根据我们的分析,如果数据原先是有效的现在改成无效的,需要从redis中删除,如果是现在有效的但是改变的其他的内容或者是从无效改成有效的都是添加或者更新到redis
        Integer state = mapping.getState();//获取状态
        NotifyMQBean notifyMQBean = new NotifyMQBean();
        notifyMQBean.setKey(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName());
        notifyMQBean.setFunctionType(FunctionType.API_MAPPING);
        if (0 == state) {
            notifyMQBean.setOperatorType(OperatorType.DELETE);
            //删除
            cacheService.del(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName());//从缓存中删除
            //TODO  发送mq消息给网关
        } else {
            //更新
            try {
                notifyMQBean.setOperatorType(OperatorType.UPDATE);
                cacheService.hSets(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName(), objectMapper.readValue(objectMapper.writeValueAsString(mapping), Map.class));
                //TODO  发送mq消息给网关
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        MessageChannel channel = notifyGateWayMQBean.message_channel();

        channel.send(new GenericMessage<NotifyMQBean>(notifyMQBean));
    }

    @Override
    public PageInfo<ApiMapping> getMappingList(ApiMapping criteria, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return new PageInfo<>(apiMappingMapper.getMappingList(criteria));
    }

    @Override
    public ApiMapping getMappingById(int id) {
        return apiMappingMapper.getMappingById(id);
    }

    @Override
    public void deleteMapping(int[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (int id : ids) {
            ApiMapping mapping = apiMappingMapper.getMappingById(id);
            if (mapping != null) {
                mapping.setState(0);
                apiMappingMapper.updateApiMapping(mapping);
                cacheService.del(ApiTagInRedis.API_MAPPING + mapping.getGatewayApiName());//从缓存中删除
                //TODO  发送mq消息给网关
            }
        }

    }

}



